﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using Medecine.Interface.Login;
using Medecine.Interface.Users;
using Medecine.Interface.Patients;
using GalaSoft.MvvmLight.Command;
using Medecine.DataAccess.Users;
using System.Reactive.Linq;
using System;
using System.Reactive;

namespace Medecine.Interface.Main
{
	public class MainViewModel : ViewModelBase
	{
		public MainViewModel()
		{
			_loginVm = new LoginViewModel();
			_logoutCommand = new RelayCommand(Logout);
			_exitCommand = new RelayCommand(Exit);

			Messenger.Default.Register<LoginMessage>(this, HandleLoginMessage);
		}

		#region Active user

		private UserViewModel _activeUser;
		public UserViewModel ActiveUser
		{
			get { return _activeUser; }
			set
			{
				if (_activeUser == value)
					return;
				_activeUser = value;
				RaisePropertyChanged("ActiveUser");
			}
		}

		public bool CanInvokeCommand()
		{
			return (ActiveUser != null) && (ActiveUser.Role != "Infirmière");
		}

		#region Login

		private LoginViewModel _loginVm = null;
		public LoginViewModel LoginVm
		{
			get { return _loginVm; }
			private set
			{
				_loginVm = value;
				RaisePropertyChanged("LoginVm");
			}
		}

		private void HandleLoginMessage(LoginMessage message)
		{
			ActiveUser = new UserViewModel(message.Content);
			Messenger.Default.Unregister<LoginMessage>(this);
			LoginVm.Cleanup();
			LoginVm = null;
			UsersVm = new UserListViewModel();
			UsersVm.GetUserList();
			Patients = new PatientListViewModel();
			Patients.LoadPatientList();
			Messenger.Default.Send(new GoToViewMessage(ViewName.Home));
		}

		#endregion // Login

		#region Logout

		private readonly RelayCommand _logoutCommand;
		public RelayCommand LogoutCommand { get { return _logoutCommand; } }

		private void Logout()
		{
			using (ServiceUserClient service = new ServiceUserClient())
			{
				Observable.FromAsyncPattern<string>(service.BeginDisconnect, service.EndDisconnect)
					.Invoke(ActiveUser.UserName)
					.ObserveOnDispatcher()
					.Subscribe(LogoutCompleted, (e) => Messenger.Default.Send(new ErrorMessage(e.Message)));
			}
		}

		private void LogoutCompleted(Unit unit)
		{
			ActiveUser = null;
			Messenger.Default.Send(new GoToViewMessage(ViewName.Login));
			LoginVm = new LoginViewModel();
			Messenger.Default.Register<LoginMessage>(this, HandleLoginMessage);
		}

		#endregion // Logout

		#endregion // Active user

		#region Users

		private UserListViewModel _usersVm;
		public UserListViewModel UsersVm
		{
			get { return _usersVm; }
			private set
			{
				_usersVm = value;
				RaisePropertyChanged("UsersVm");
			}
		}

		#endregion // Users

		#region Patients

		private PatientListViewModel _patients;
		public PatientListViewModel Patients
		{
			get { return _patients; }
			private set
			{
				if (_patients == value)
					return;
				_patients = value;
				RaisePropertyChanged("Patients");
			}
		}

		#endregion // Patients

		#region Exit

		private readonly RelayCommand _exitCommand;
		public RelayCommand ExitCommand { get { return _exitCommand; } }

		private void Exit()
		{
			ViewModelLocator.Cleanup();
		}

		#endregion // Exit
	}
}